/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnfUbicacionPallet.java
 *
 * Created on 16/10/2011, 01:59:46 PM
 */

package visual.Ingreso;

import bentity.beAlmacen;
import bentity.beCombo;
import bentity.beItem;
import bentity.beLote;
import bentity.bePallet;
import bentity.beProducto;
import bentity.beUsuario;
import blogic.blAlmacen;
import blogic.blHelper;
import blogic.blLote;
import blogic.blPallet;
import java.awt.Dimension;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
//import java.util.Date;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import visual.Seguridad.visualHelper;

/**
 *
 * @author chokewanka
 */
public class pnfUbicacionPallet extends javax.swing.JPanel {
    private DefaultTableModel dtm;
    private int raCol = 0;
    private String entradas[]=new String[2];
    private ArrayList<beLote> arrLotes=new ArrayList<beLote>();
    private Double[] datosPallet=new Double[3];
    private int indiceLote=0;

    public int getrFila() {
        return rFila;
    }

    public void setrFila(int rFila) {
        this.rFila = rFila;
    }

    public int getRaCol() {
        return raCol;
    }

    public void setRaCol(int raCol) {
        this.raCol = raCol;
    }

    public int getTamR() {
        return tamR;
    }

    public void setTamR(int tamR) {
        this.tamR = tamR;
    }



    private int rFila = 0;
    private int tamR = 0;
    private beAlmacen alma =new beAlmacen();
    private boolean nuevo=false;
    private beUsuario usuario;

    /** Creates new form pnfUbicacionPallet */
    public pnfUbicacionPallet(beUsuario usuario) {
        initComponents();
        this.txtAltoCaja.setTransferHandler(null);
        this.txtAnchoCaja.setTransferHandler(null);
        this.txtAnchoPallet.setTransferHandler(null);
        this.txtCajasPorPallet.setTransferHandler(null);
        this.txtCantidadPallets.setTransferHandler(null);
        this.txtCodigo.setTransferHandler(null);
        this.txtLargoCaja.setTransferHandler(null);
        this.txtLargoPallet.setTransferHandler(null);
        this.txtPesoCaja.setTransferHandler(null);
        this.txtPesoPallet.setTransferHandler(null);
        this.txtProducto.setTransferHandler(null);
        try{
            this.usuario=usuario;
            this.dtm = (DefaultTableModel)this.tblPallets.getModel();
            this.tblPallets.getColumnModel().getColumn(0).setPreferredWidth(50);
            this.tblPallets.getColumnModel().getColumn(1).setPreferredWidth(105);
            this.tblPallets.getColumnModel().getColumn(2).setPreferredWidth(90);
            this.tblPallets.getColumnModel().getColumn(3).setPreferredWidth(105);
            this.tblPallets.getColumnModel().getColumn(4).setPreferredWidth(50);
            this.tblPallets.getColumnModel().getColumn(5).setPreferredWidth(50);

            blHelper blh=new blHelper();
            ArrayList<beCombo> comboObjeto=new ArrayList<beCombo>();

            comboObjeto=(new blHelper()).llenaCombo("PALLET");
            this.cbxTipoPallet.setModel(blh.ComboBoxItem(comboObjeto));

            blAlmacen bla=new blAlmacen();
            alma=bla.fObtenerAlmacen();
            this.setRaCol(alma.getFilas());
            this.setrFila(alma.getRacksXFila());

            llenarDatosDePallet();

            this.dtcVencimiento.setDate(new Date(Calendar.getInstance().getTime().getTime()));



        }catch(Exception e){}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        lblTitulo = new javax.swing.JLabel();
        btnBorrarTodos = new javax.swing.JButton();
        btnUbicar = new javax.swing.JButton();
        lblLotes = new javax.swing.JLabel();
        pnlLotes = new javax.swing.JPanel();
        lblProducto = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        txtCantidadPallets = new javax.swing.JTextField();
        lblCantPallets = new javax.swing.JLabel();
        btnBuscarProducto = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        txtProducto = new javax.swing.JTextField();
        pnlPalletsss = new javax.swing.JPanel();
        txtCajasPorPallet = new javax.swing.JTextField();
        lblCajasPorPallet = new javax.swing.JLabel();
        cbxTipoPallet = new javax.swing.JComboBox();
        txtTipoPallet = new javax.swing.JLabel();
        txtAnchoPallet = new javax.swing.JTextField();
        lblAnchoPallet = new javax.swing.JLabel();
        lblLargoPallet = new javax.swing.JLabel();
        txtLargoPallet = new javax.swing.JTextField();
        lblPesoPallet = new javax.swing.JLabel();
        txtPesoPallet = new javax.swing.JTextField();
        pnlCajas = new javax.swing.JPanel();
        txtAnchoCaja = new javax.swing.JTextField();
        txtLargoCaja = new javax.swing.JTextField();
        txtAltoCaja = new javax.swing.JTextField();
        lblAlto = new javax.swing.JLabel();
        lblLargo = new javax.swing.JLabel();
        lblAncho = new javax.swing.JLabel();
        dtcVencimiento = new com.toedter.calendar.JDateChooser();
        lblVencimiento = new javax.swing.JLabel();
        txtPesoCaja = new javax.swing.JTextField();
        lblPesoCaja = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblPallets = new javax.swing.JTable();

        jPanel2.setBackground(new java.awt.Color(178, 166, 146));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 479, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 361, Short.MAX_VALUE)
        );

        setPreferredSize(new java.awt.Dimension(1160, 544));
        setLayout(null);

        lblTitulo.setFont(new java.awt.Font("Dialog", 0, 36));
        lblTitulo.setText("Ingreso de Lotes");
        add(lblTitulo);
        lblTitulo.setBounds(290, 10, 330, 43);

        btnBorrarTodos.setBackground(new java.awt.Color(240, 235, 226));
        btnBorrarTodos.setFont(new java.awt.Font("Dialog", 0, 12));
        btnBorrarTodos.setForeground(new java.awt.Color(0, 0, 0));
        btnBorrarTodos.setText("Borrar Todos");
        btnBorrarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarTodosActionPerformed(evt);
            }
        });
        add(btnBorrarTodos);
        btnBorrarTodos.setBounds(120, 580, 130, 27);

        btnUbicar.setBackground(new java.awt.Color(240, 235, 226));
        btnUbicar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnUbicar.setForeground(new java.awt.Color(0, 0, 0));
        btnUbicar.setText("Ingresar");
        btnUbicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbicarActionPerformed(evt);
            }
        });
        add(btnUbicar);
        btnUbicar.setBounds(780, 580, 150, 27);

        lblLotes.setFont(new java.awt.Font("Dialog", 0, 14));
        lblLotes.setForeground(new java.awt.Color(0, 0, 0));
        lblLotes.setText("Lotes");
        add(lblLotes);
        lblLotes.setBounds(20, 370, 60, 17);

        pnlLotes.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Lote", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14))); // NOI18N
        pnlLotes.setLayout(null);

        lblProducto.setFont(new java.awt.Font("Dialog", 0, 12));
        lblProducto.setText("Producto*");
        pnlLotes.add(lblProducto);
        lblProducto.setBounds(140, 30, 100, 15);

        txtCodigo.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        txtCodigo.setEnabled(false);
        pnlLotes.add(txtCodigo);
        txtCodigo.setBounds(210, 30, 50, 25);

        txtCantidadPallets.setFont(new java.awt.Font("Dialog", 0, 12));
        txtCantidadPallets.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCantidadPalletsKeyTyped(evt);
            }
        });
        pnlLotes.add(txtCantidadPallets);
        txtCantidadPallets.setBounds(610, 30, 120, 25);

        lblCantPallets.setFont(new java.awt.Font("Dialog", 0, 12));
        lblCantPallets.setForeground(new java.awt.Color(0, 0, 0));
        lblCantPallets.setText("Cantidad de pallets*");
        pnlLotes.add(lblCantPallets);
        lblCantPallets.setBounds(470, 30, 140, 15);

        btnBuscarProducto.setBackground(new java.awt.Color(240, 235, 226));
        btnBuscarProducto.setFont(new java.awt.Font("Dialog", 0, 12));
        btnBuscarProducto.setForeground(new java.awt.Color(0, 0, 0));
        btnBuscarProducto.setText("...");
        btnBuscarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarProductoActionPerformed(evt);
            }
        });
        pnlLotes.add(btnBuscarProducto);
        btnBuscarProducto.setBounds(430, 30, 30, 27);

        btnLimpiar.setBackground(new java.awt.Color(240, 235, 226));
        btnLimpiar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnLimpiar.setForeground(new java.awt.Color(0, 0, 0));
        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });
        pnlLotes.add(btnLimpiar);
        btnLimpiar.setBounds(330, 270, 100, 27);

        txtProducto.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        txtProducto.setEnabled(false);
        pnlLotes.add(txtProducto);
        txtProducto.setBounds(260, 30, 170, 25);

        pnlPalletsss.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Pallet", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14))); // NOI18N
        pnlPalletsss.setLayout(null);

        txtCajasPorPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        txtCajasPorPallet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCajasPorPalletActionPerformed(evt);
            }
        });
        txtCajasPorPallet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCajasPorPalletKeyTyped(evt);
            }
        });
        pnlPalletsss.add(txtCajasPorPallet);
        txtCajasPorPallet.setBounds(140, 20, 140, 25);

        lblCajasPorPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        lblCajasPorPallet.setForeground(new java.awt.Color(0, 0, 0));
        lblCajasPorPallet.setText("Cantidad de cajas *");
        pnlPalletsss.add(lblCajasPorPallet);
        lblCajasPorPallet.setBounds(10, 20, 140, 15);

        cbxTipoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        cbxTipoPallet.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxTipoPalletItemStateChanged(evt);
            }
        });
        cbxTipoPallet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxTipoPalletActionPerformed(evt);
            }
        });
        pnlPalletsss.add(cbxTipoPallet);
        cbxTipoPallet.setBounds(140, 50, 180, 25);

        txtTipoPallet.setFont(new java.awt.Font("DejaVu Sans", 0, 12));
        txtTipoPallet.setForeground(new java.awt.Color(0, 0, 0));
        txtTipoPallet.setText("Tipo de pallet*");
        pnlPalletsss.add(txtTipoPallet);
        txtTipoPallet.setBounds(10, 50, 140, 15);

        txtAnchoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        txtAnchoPallet.setEnabled(false);
        txtAnchoPallet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAnchoPalletActionPerformed(evt);
            }
        });
        txtAnchoPallet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtAnchoPalletKeyTyped(evt);
            }
        });
        pnlPalletsss.add(txtAnchoPallet);
        txtAnchoPallet.setBounds(140, 80, 90, 25);

        lblAnchoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        lblAnchoPallet.setForeground(new java.awt.Color(0, 0, 0));
        lblAnchoPallet.setText("Ancho (cm)");
        pnlPalletsss.add(lblAnchoPallet);
        lblAnchoPallet.setBounds(10, 80, 80, 15);

        lblLargoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        lblLargoPallet.setForeground(new java.awt.Color(0, 0, 0));
        lblLargoPallet.setText("Largo (cm)");
        pnlPalletsss.add(lblLargoPallet);
        lblLargoPallet.setBounds(10, 110, 90, 15);

        txtLargoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        txtLargoPallet.setEnabled(false);
        txtLargoPallet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtLargoPalletKeyTyped(evt);
            }
        });
        pnlPalletsss.add(txtLargoPallet);
        txtLargoPallet.setBounds(140, 110, 90, 25);

        lblPesoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        lblPesoPallet.setForeground(new java.awt.Color(0, 0, 0));
        lblPesoPallet.setText("Peso maximo (Kg)");
        pnlPalletsss.add(lblPesoPallet);
        lblPesoPallet.setBounds(10, 140, 140, 15);

        txtPesoPallet.setFont(new java.awt.Font("Dialog", 0, 12));
        txtPesoPallet.setEnabled(false);
        txtPesoPallet.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPesoPalletKeyTyped(evt);
            }
        });
        pnlPalletsss.add(txtPesoPallet);
        txtPesoPallet.setBounds(140, 140, 90, 25);

        pnlLotes.add(pnlPalletsss);
        pnlPalletsss.setBounds(50, 80, 330, 180);

        pnlCajas.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Caja", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 14))); // NOI18N
        pnlCajas.setLayout(null);

        txtAnchoCaja.setFont(new java.awt.Font("Dialog", 0, 12));
        txtAnchoCaja.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtAnchoCajaKeyTyped(evt);
            }
        });
        pnlCajas.add(txtAnchoCaja);
        txtAnchoCaja.setBounds(100, 110, 90, 25);

        txtLargoCaja.setFont(new java.awt.Font("Dialog", 0, 12));
        txtLargoCaja.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtLargoCajaKeyTyped(evt);
            }
        });
        pnlCajas.add(txtLargoCaja);
        txtLargoCaja.setBounds(100, 80, 90, 25);

        txtAltoCaja.setFont(new java.awt.Font("Dialog", 0, 12));
        txtAltoCaja.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtAltoCajaKeyTyped(evt);
            }
        });
        pnlCajas.add(txtAltoCaja);
        txtAltoCaja.setBounds(100, 50, 90, 25);

        lblAlto.setFont(new java.awt.Font("Dialog", 0, 12));
        lblAlto.setForeground(new java.awt.Color(0, 0, 0));
        lblAlto.setText("Alto (cm)*");
        pnlCajas.add(lblAlto);
        lblAlto.setBounds(10, 50, 80, 15);

        lblLargo.setFont(new java.awt.Font("Dialog", 0, 12));
        lblLargo.setForeground(new java.awt.Color(0, 0, 0));
        lblLargo.setText("Largo (cm)*");
        pnlCajas.add(lblLargo);
        lblLargo.setBounds(10, 80, 80, 15);

        lblAncho.setFont(new java.awt.Font("Dialog", 0, 12));
        lblAncho.setForeground(new java.awt.Color(0, 0, 0));
        lblAncho.setText("Ancho (cm)*");
        pnlCajas.add(lblAncho);
        lblAncho.setBounds(10, 110, 90, 15);
        pnlCajas.add(dtcVencimiento);
        dtcVencimiento.setBounds(100, 20, 200, 27);

        lblVencimiento.setFont(new java.awt.Font("Dialog", 0, 12));
        lblVencimiento.setForeground(new java.awt.Color(0, 0, 0));
        lblVencimiento.setText("Vencimiento*");
        pnlCajas.add(lblVencimiento);
        lblVencimiento.setBounds(10, 20, 140, 15);

        txtPesoCaja.setFont(new java.awt.Font("Dialog", 0, 12));
        txtPesoCaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPesoCajaActionPerformed(evt);
            }
        });
        txtPesoCaja.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPesoCajaKeyTyped(evt);
            }
        });
        pnlCajas.add(txtPesoCaja);
        txtPesoCaja.setBounds(100, 140, 90, 25);

        lblPesoCaja.setFont(new java.awt.Font("Dialog", 0, 12));
        lblPesoCaja.setForeground(new java.awt.Color(0, 0, 0));
        lblPesoCaja.setText("Peso (Kg)*");
        pnlCajas.add(lblPesoCaja);
        lblPesoCaja.setBounds(10, 140, 140, 15);

        pnlLotes.add(pnlCajas);
        pnlCajas.setBounds(550, 80, 310, 180);

        btnAgregar.setBackground(new java.awt.Color(240, 235, 226));
        btnAgregar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnAgregar.setForeground(new java.awt.Color(0, 0, 0));
        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        pnlLotes.add(btnAgregar);
        btnAgregar.setBounds(500, 270, 100, 27);

        add(pnlLotes);
        pnlLotes.setBounds(10, 60, 920, 310);

        btnBorrar.setBackground(new java.awt.Color(240, 235, 226));
        btnBorrar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnBorrar.setForeground(new java.awt.Color(0, 0, 0));
        btnBorrar.setText("Borrar");
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });
        add(btnBorrar);
        btnBorrar.setBounds(10, 580, 100, 27);

        tblPallets.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Num lote", "Producto", "# Pallets", "Tipo pallet", "Cajas por pallet", "Vencimiento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPallets.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblPallets.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tblPallets);

        add(jScrollPane2);
        jScrollPane2.setBounds(10, 390, 920, 180);
    }// </editor-fold>//GEN-END:initComponents

    private void btnBorrarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarTodosActionPerformed
        int result =visualHelper.mostrarPreguntaSiNo("¿Está seguro de borrar todos los pallets?");
        if(result==JOptionPane.NO_OPTION){
            return;
        }
        while(this.dtm.getRowCount()>0){
            this.dtm.removeRow(0);
            this.arrLotes=new ArrayList<beLote>();
            this.indiceLote=0;
        }
    }//GEN-LAST:event_btnBorrarTodosActionPerformed

    private void btnBuscarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarProductoActionPerformed
        dlgBuscarProducto bpd = new dlgBuscarProducto(null,true);
        bpd.setVisible(true);
        beProducto producto=bpd.getProdElegido();
        bpd.dispose();
        if(producto==null){
            this.txtProducto.setText("");
            this.txtCodigo.setText("");
        }
        else{
        this.txtProducto.setText(producto.getNombre());
        this.txtCodigo.setText(String.valueOf(producto.getIdProducto()));
        }
        
}//GEN-LAST:event_btnBuscarProductoActionPerformed

    private void btnUbicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbicarActionPerformed
        try{
            if(this.dtm.getRowCount()<=0){
                JOptionPane.showMessageDialog(this, "Debe agregar al menos un lote.", "Advertencia", JOptionPane.WARNING_MESSAGE);
                return;
            }

            int result =visualHelper.mostrarPreguntaSiNo("¿Está seguro de ingresar los pallets?");
            if(result==JOptionPane.NO_OPTION){
                return;
            }

            this.limpiar();

            dlgIngresoOptimoProductos ing = new dlgIngresoOptimoProductos(this, true, this.arrLotes,usuario);
            ing.setMinimumSize(new Dimension(600, 600));
            ing.setVisible(true);
            ing.setVisible(false);
            ing.dispose();
            this.arrLotes=new ArrayList<beLote>();
            this.limpiar();

        }catch(Exception e){
            e.printStackTrace();
            visualHelper.mostrarMensajeError("Ocurrio un error");
        }
    }//GEN-LAST:event_btnUbicarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        this.limpiar();
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        int result =visualHelper.mostrarPreguntaSiNo("¿Está seguro de eliminar el pallet?");
        if(result==JOptionPane.NO_OPTION)
            return;
        if (this.tblPallets.getSelectedRow() == -1)
            return;

        for(int i=0;i<this.arrLotes.size();i++){
            if(this.arrLotes.get(i).getIdLote()==Integer.parseInt(this.dtm.getValueAt(this.tblPallets.getSelectedRow(), 0).toString())){
                arrLotes.remove(i);
                break;
            }
        }
        this.dtm.removeRow(this.tblPallets.getSelectedRow());
    }//GEN-LAST:event_btnBorrarActionPerformed

    private void txtCantidadPalletsKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadPalletsKeyTyped
        String strCantPal = this.txtCantidadPallets.getText();
        if (strCantPal.length() >= 10)
            visualHelper.ningunCaracterEnTextField(evt);
        else
        visualHelper.soloDigitosEnTextField(evt);
    }//GEN-LAST:event_txtCantidadPalletsKeyTyped

    private void txtPesoPalletKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesoPalletKeyTyped
        String strCadena = this.txtPesoPallet.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtPesoPalletKeyTyped

    private void txtCajasPorPalletKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCajasPorPalletKeyTyped
        String strCajXPal = this.txtCajasPorPallet.getText();
        if (strCajXPal.length() >= 10)
            visualHelper.ningunCaracterEnTextField(evt);
        else
        visualHelper.soloDigitosEnTextField(evt);
    }//GEN-LAST:event_txtCajasPorPalletKeyTyped

    private void txtAltoCajaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAltoCajaKeyTyped
        String strCadena = this.txtAltoCaja.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtAltoCajaKeyTyped

    private void txtLargoCajaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLargoCajaKeyTyped
        String strCadena = this.txtLargoCaja.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtLargoCajaKeyTyped

    private void txtAnchoCajaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAnchoCajaKeyTyped
        String strCadena = this.txtAnchoCaja.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtAnchoCajaKeyTyped

    private void txtAnchoPalletKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAnchoPalletKeyTyped
        String strCadena = this.txtAnchoPallet.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtAnchoPalletKeyTyped

    private void txtLargoPalletKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLargoPalletKeyTyped
        String strCadena = this.txtLargoPallet.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtLargoPalletKeyTyped

    private void txtCajasPorPalletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCajasPorPalletActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCajasPorPalletActionPerformed

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        blHelper blh=new blHelper();

        //NULOS

        if(this.txtCodigo.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("Elija un producto");
        return;
        }

        if(this.txtCantidadPallets.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese numero de pallets");
        return;
        }

        if(this.txtAnchoPallet.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese ancho de pallet");
        return;
        }

        if(this.txtLargoPallet.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese largo de pallet");
        return;
        }

        if(this.txtPesoPallet.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese peso de pallet");
        return;
        }

        if(this.txtCajasPorPallet.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese cantidad de cajas");
        return;
        }

        if(this.txtAnchoCaja.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese ancho de caja");
        return;
        }

        if(this.txtAltoCaja.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese alto de caja");
        return;
        }

        if(this.txtLargoCaja.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese largo de caja");
        return;
        }

        if(this.txtPesoCaja.getText().equals("")){
        visualHelper.mostrarMensajeAdvertencia("ingrese peso de caja");
        return;
        }

        if(this.dtcVencimiento.getDate()==null){
        visualHelper.mostrarMensajeAdvertencia("Elija una fecha de vencimiento");
        return;
        }

        //VALORES


        Calendar calendario=Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");


        this.dtcVencimiento.setDate(blHelper.setTimeToMidnight(new Date(this.dtcVencimiento.getDate().getTime())));
        calendario.setTime(blHelper.setTimeToMidnight(new Date(calendario.getTime().getTime())));

        if(this.dtcVencimiento.getDate().before(Calendar.getInstance().getTime())){

            if(!this.dtcVencimiento.getDate().equals(Calendar.getInstance().getTime())){
                visualHelper.mostrarMensajeAdvertencia("No puede ingresar productos vencidos ni que vencen hoy");
                //visualHelper.mostrarMensajeAdvertencia("Elija una fecha mayor que el dia actual ("+sdf.format(calendario.getTime())+")");
                return;
            }

        }

        if(Double.parseDouble(this.txtCantidadPallets.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("La cantidad de pallets debe ser mayor a 0");
            return;
        }

        if(Double.parseDouble(this.txtCajasPorPallet.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("La cantidad de cajas por pallets debe ser mayor a 0");
            return;
        }

        if(Double.parseDouble(this.txtAltoCaja.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("El alto de la caja debe ser mayor a 0");
            return;
        }

        if(Double.parseDouble(this.txtLargoCaja.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("El largo de la caja debe ser mayor a 0");
            return;
        }

        if(Double.parseDouble(this.txtAnchoCaja.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("El ancho de la caja debe ser mayor a 0");
            return;
        }

        if(Double.parseDouble(this.txtPesoCaja.getText())==0){
            visualHelper.mostrarMensajeAdvertencia("El peso de la caja debe ser mayor a 0");
            return;
        }
//
//        if(!blh.caracteresSoloDigitos(this.txtCantidadPallets.getText())){
//        visualHelper.mostrarMensajeAdvertencia("Elija una cantidad de pallets");
//        return;
//        }
//
//        if(this.txtCantidadPallets.getText().equals("") || !blh.cadenaSoloDouble(this.txtPeso.getText())){
//        visualHelper.mostrarMensajeAdvertencia("Ingrese un peso de lote");
//        return;
//        }



        try{


            int idLote=this.indiceLote+1;
            this.indiceLote++;
            int idProducto=Integer.parseInt(this.txtCodigo.getText());
            String nombreProducto=this.txtProducto.getText();
            int cantidadPallets=Integer.parseInt(this.txtCantidadPallets.getText());

            double anchoPallet=Double.parseDouble(this.txtAnchoPallet.getText());
            double largoPallet=Double.parseDouble(this.txtLargoPallet.getText());
            int tipoPallet= (new blHelper().dameIdEstado(((beItem)this.cbxTipoPallet.getSelectedItem()).getDescription(), "PALLET")) ;
            int numCajasPorPallet = Integer.parseInt(this.txtCajasPorPallet.getText());
            double pesoPallet=Double.parseDouble(this.txtPesoPallet.getText());

            double altoCaja=Double.parseDouble(this.txtAltoCaja.getText());
            double anchoCaja=Double.parseDouble(this.txtAnchoCaja.getText());
            double largoCaja=Double.parseDouble(this.txtLargoCaja.getText());
            double pesoCaja=Double.parseDouble(this.txtPesoCaja.getText());
            Date fecha=visualHelper.obtenerFechaCalendario(this.dtcVencimiento);


            if(pesoCaja*numCajasPorPallet>pesoPallet){
                visualHelper.mostrarMensajeAdvertencia("El peso total de cajas supera el peso máximo soportado por el pallet.");
                return;
            }

            if(anchoCaja>anchoPallet){
                if((anchoCaja>largoPallet) || (largoCaja>anchoPallet)){
                    visualHelper.mostrarMensajeAdvertencia("El área superficial de la caja excede el área del pallet");
                    return;
                }
            }
            else if(largoCaja>largoPallet){
                if((anchoCaja>largoPallet) || (largoCaja>anchoPallet)){
                    visualHelper.mostrarMensajeAdvertencia("El área superficial de la caja excede el área del pallet");
                    return;
                }
            }

            //LLENAR EL ARREGLO DE LOTES

            ArrayList<bePallet> arrPallets=new ArrayList<bePallet>();

            beLote lote=new beLote();
            lote.setIdLote(idLote);
            lote.setIdProducto(idProducto);
            lote.setNombreProducto(nombreProducto);
            lote.setFechaVencimiento(fecha);
            lote.setAltoCaja(altoCaja);
            lote.setAnchoCaja(anchoCaja);
            lote.setLargoCaja(largoCaja);
            lote.setPesoCaja(pesoCaja);
            lote.setCantidadPallets(cantidadPallets);
            lote.setCajasPorPallet(numCajasPorPallet);
            arrPallets=new ArrayList<bePallet>();

            for(int j=0;j<cantidadPallets;j++){
                bePallet pallet=new bePallet(-1);
                pallet.setIdProducto(idProducto);
                pallet.setAncho(anchoPallet);
                pallet.setLargo(largoPallet);
                pallet.setPeso(pesoPallet);
                pallet.setTipo_pallet(tipoPallet);
                pallet.setNroPallet(j+1);
                pallet.setCantCajas(numCajasPorPallet);
                arrPallets.add(pallet);
             }
             lote.setPallets(arrPallets);

             arrLotes.add(lote);


            //LLENAR TABLA


            Object [] datos = new Object [6];
            datos[0] = idLote;
            datos[1] = nombreProducto;
            datos[2] = cantidadPallets;
            datos[3] = ((beItem)this.cbxTipoPallet.getSelectedItem()).getDescription();
            datos[4] = numCajasPorPallet;
            datos[5] = sdf.format(fecha);

            dtm.addRow(datos);



        }catch(Exception e){
            e.printStackTrace();
            visualHelper.mostrarMensajeError("Ha ocurrido un error agregando el lote");
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void txtAnchoPalletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAnchoPalletActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAnchoPalletActionPerformed

    private void txtPesoCajaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPesoCajaKeyTyped
        String strCadena = this.txtPesoCaja.getText();
        int i = strCadena.indexOf(".");
        if (i==-1){
            if (strCadena.length() > 7)
                visualHelper.soloPuntos(evt);
            else
                visualHelper.soloDigitosYPunto(evt);
        }
        else{
            if(strCadena.length()>(i+2)){
                visualHelper.ningunCaracterEnTextField(evt);
            }
            else{
            visualHelper.soloDigitosEnTextField(evt);
            }
        }
    }//GEN-LAST:event_txtPesoCajaKeyTyped

    private void txtPesoCajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPesoCajaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPesoCajaActionPerformed

    private void cbxTipoPalletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxTipoPalletActionPerformed
        try{
            llenarDatosDePallet();
        }catch(Exception e){
            e.printStackTrace();
            visualHelper.mostrarMensajeError("Ha ocurrido un error");
        }
    }//GEN-LAST:event_cbxTipoPalletActionPerformed

    private void cbxTipoPalletItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxTipoPalletItemStateChanged
        
    }//GEN-LAST:event_cbxTipoPalletItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnBorrarTodos;
    private javax.swing.JButton btnBuscarProducto;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnUbicar;
    private javax.swing.JComboBox cbxTipoPallet;
    private com.toedter.calendar.JDateChooser dtcVencimiento;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAlto;
    private javax.swing.JLabel lblAncho;
    private javax.swing.JLabel lblAnchoPallet;
    private javax.swing.JLabel lblCajasPorPallet;
    private javax.swing.JLabel lblCantPallets;
    private javax.swing.JLabel lblLargo;
    private javax.swing.JLabel lblLargoPallet;
    private javax.swing.JLabel lblLotes;
    private javax.swing.JLabel lblPesoCaja;
    private javax.swing.JLabel lblPesoPallet;
    private javax.swing.JLabel lblProducto;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JLabel lblVencimiento;
    private javax.swing.JPanel pnlCajas;
    private javax.swing.JPanel pnlLotes;
    private javax.swing.JPanel pnlPalletsss;
    private javax.swing.JTable tblPallets;
    private javax.swing.JTextField txtAltoCaja;
    private javax.swing.JTextField txtAnchoCaja;
    private javax.swing.JTextField txtAnchoPallet;
    private javax.swing.JTextField txtCajasPorPallet;
    private javax.swing.JTextField txtCantidadPallets;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtLargoCaja;
    private javax.swing.JTextField txtLargoPallet;
    private javax.swing.JTextField txtPesoCaja;
    private javax.swing.JTextField txtPesoPallet;
    private javax.swing.JTextField txtProducto;
    private javax.swing.JLabel txtTipoPallet;
    // End of variables declaration//GEN-END:variables

    private void limpiar(){
        this.txtCodigo.setText("");
        this.txtProducto.setText("");
        this.txtCantidadPallets.setText("");        
        this.txtCajasPorPallet.setText("");
        this.txtAltoCaja.setText("");
        this.txtAnchoCaja.setText("");
        this.txtLargoCaja.setText("");
        this.txtPesoCaja.setText("");

        for(int i=0;i<=dtm.getRowCount()-1;){
            dtm.removeRow(0);
        }

        this.dtcVencimiento.setDate(new Date(Calendar.getInstance().getTime().getTime()));

    }

    private void llenarDatosDePallet() throws Exception{

        datosPallet=(new blPallet().dameDatosDelTipoPallet(((beItem)this.cbxTipoPallet.getSelectedItem()).getDescription()));
        this.txtAnchoPallet.setText(String.valueOf(datosPallet[0]));
        this.txtLargoPallet.setText(String.valueOf(datosPallet[1]));
        this.txtPesoPallet.setText(String.valueOf(datosPallet[2]));

    }
}
